package org.administer.contaroller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.administer.dao.CrmCustomerDao;
import org.administer.dao.CrmDictionariesDao;
import org.administer.dao.CrmOrderDetailsDao;
import org.administer.pojo.CrmCustomers;
import org.administer.pojo.CrmDictionaries;
import org.administer.pojo.CrmOrderDetails;
import org.administer.pojo.CrmUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customers")
public class CustomersController {
    @Autowired
    private CrmCustomerDao crmCustomerDao;
    @Autowired
    private CrmDictionariesDao crmDictionariesDao;
    @Autowired
    private CrmOrderDetailsDao crmOrderDetailsDao;
    @RequestMapping("/index")
    public String index() throws Exception{
        return "customer/cusindex";
    }
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, CrmCustomers crmCustomers, HttpSession session) throws Exception{
        Map<String,Object> map=new HashMap<>();
        CrmUser crmUser= (CrmUser) session.getAttribute("login_user");
        crmCustomers.setUserId(crmUser.getId());
        Page<CrmCustomers> pager= PageHelper.startPage(page,limit);
        List<CrmCustomers> list=crmCustomerDao.getlist(crmCustomers);
        List<CrmDictionaries> dengjji=crmDictionariesDao.getlevel();
        session.setAttribute("dengjji",dengjji);
        map.put("code",0);
        map.put("msg",null);
        map.put("data",list);
        map.put("count",pager.getTotal());
        return map;
    }
    @RequestMapping("/addform")
    @RequiresPermissions("customers:addform")
    public String addform(HttpSession session) throws Exception{
        List<CrmDictionaries> dengjji=crmDictionariesDao.getlevel();
        session.setAttribute("dengjji",dengjji);
        List<CrmDictionaries> sa=crmDictionariesDao.getsatisfied();
        session.setAttribute("sa",sa);
        List<CrmDictionaries> cre=crmDictionariesDao.getcredit();
        session.setAttribute("cre",cre);
        List<CrmDictionaries> zhuangtai=crmDictionariesDao.getzhuangtai();
        session.setAttribute("zhuangtai",zhuangtai);
        List<CrmDictionaries> fenpei=crmDictionariesDao.getstat();
        session.setAttribute("fenpei",fenpei);
        return "customer/addform";
    }
    @RequestMapping("/selectfrom")
    public String selectfrom(HttpSession session) throws Exception{
        List<CrmDictionaries> dengjji=crmDictionariesDao.getlevel();
        session.setAttribute("dengjji",dengjji);
        List<CrmDictionaries> sa=crmDictionariesDao.getsatisfied();
        session.setAttribute("sa",sa);
        List<CrmDictionaries> cre=crmDictionariesDao.getcredit();
        session.setAttribute("cre",cre);
        List<CrmDictionaries> zhuangtai=crmDictionariesDao.getzhuangtai();
        session.setAttribute("zhuangtai",zhuangtai);
        List<CrmDictionaries> fenpei=crmDictionariesDao.getstat();
        session.setAttribute("fenpei",fenpei);
        return "customer/selectfrom";
    }
    @RequestMapping("/search")
    @ResponseBody
    public Map<String,Object> search() throws Exception{
        Map<String,Object> map=new HashMap<>();
        map.put("data",crmDictionariesDao.getstat());
        return map;
    }
//    @RequestMapping("/satisfied")
//    @ResponseBody
//    public Map<String,Object> satisfied() throws Exception{
//        Map<String,Object> map=new HashMap<>();
//        map.put("data",crmDictionariesDao.getsatisfied());
//        return map;
//    }
//    @RequestMapping("/credit")
//    @ResponseBody
//    public Map<String,Object> credit() throws Exception{
//        Map<String,Object> map=new HashMap<>();
//        map.put("data",crmDictionariesDao.getcredit());
//        return map;
//    }
    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> add(CrmCustomers crmCustomers,HttpSession session) throws Exception{
        Map<String,Object> map=new HashMap<>();
        CrmUser crmUser= (CrmUser) session.getAttribute("login_user");
        crmCustomers.setUserId(crmUser.getId());
        crmCustomerDao.add(crmCustomers);
        return map;
    }
    @RequestMapping("/batchDelete")
    @ResponseBody
    public Map<String,Object> batchDelete(Integer[] ids) throws Exception{
        Map<String,Object> map = new HashMap<>();
        crmCustomerDao.delete(ids);
        map.put("result",true);
        return map;
    }
    @RequestMapping("/update")
    @ResponseBody
    @RequiresPermissions("customers:addform")
    public Map<String,Object> update(CrmCustomers crmCustomers,HttpSession session) throws Exception{
        Map<String,Object> map=new HashMap<>();
        CrmUser crmUser= (CrmUser) session.getAttribute("login_user");
        crmCustomers.setUserId(crmUser.getId());
        crmCustomerDao.update(crmCustomers);
        return map;
    }
    @RequestMapping("/delid")
    @ResponseBody
    public Map<String,Object> getid(int id) throws Exception{
        Map<String,Object> map=new HashMap<>();
        crmCustomerDao.del(id);
        return map;
    }
    @RequestMapping("/updateloss")
    @ResponseBody
    public Map<String,Object> updateloss(Integer aa) throws Exception{
        Map<String,Object> map=new HashMap<>();
        crmCustomerDao.udatelossss(aa);
        return map;
    }
    @RequestMapping("/updateshouhuo")
    @ResponseBody
    public Map<String,Object> updateshouhuo(Integer cusno,String Ordered) throws Exception{
        Map<String,Object> map=new HashMap<>();
        List<CrmOrderDetails> list=crmOrderDetailsDao.xiangqing(Ordered);
        System.out.println("集合"+crmOrderDetailsDao.xiangqing(Ordered).size());
        if(crmOrderDetailsDao.xiangqing(Ordered).size()==0){
            map.put("code",1);
            map.put("msg","你没有购买的商品，无法收货...");
            return map;
        }
        crmCustomerDao.updateshouhuo(cusno);
        return map;
    }
}
